﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Input;
using Microsoft.Devices.Sensors;
using Microsoft.Xna.Framework;

namespace MoveIt.Framework
{
    public class CInput
    {
        private KeyboardState m_CurKey;
        private KeyboardState m_OldKey;
        private Accelerometer m_AccelSensor;
        private Vector3 m_AccelReading;

        public Accelerometer AccelSensor
        {
            get { return m_AccelSensor; }
            set { m_AccelSensor = value; }
        }

        public Vector3 AccelReading
        {
            get { return m_AccelReading; }
            set { m_AccelReading = value; }
        }
        public KeyboardState CurKey
        {
            get { return m_CurKey; }
            set { m_CurKey = value; }
        }

        public KeyboardState OldKey
        {
            get { return m_OldKey; }
            set { m_OldKey = value; }
        }

        public CInput()
        {
            m_CurKey = Keyboard.GetState();
            m_AccelReading = new Vector3();
            m_AccelSensor = new Accelerometer();

            // Add the accelerometer event handler to the accelerometer sensor.
            m_AccelSensor.ReadingChanged += new EventHandler<AccelerometerReadingEventArgs>(AccelerometerReadingChanged);
            m_AccelSensor.Start();
        }

        public void Update()
        {
            m_OldKey = m_CurKey;
            m_CurKey = Keyboard.GetState();
        }

        public bool KeyReleased(Keys _Key)
        {
            return m_CurKey.IsKeyUp(_Key) && m_OldKey.IsKeyDown(_Key);
        }

        public bool KeyPressed(Keys _Key)
        {
            return m_CurKey.IsKeyDown(_Key) && m_OldKey.IsKeyUp(_Key);
        }

        public bool KeyDown(Keys _Key)
        {
            return m_CurKey.IsKeyDown(_Key);
        }
        public void AccelerometerReadingChanged(object sender, AccelerometerReadingEventArgs e)
        {
            m_AccelReading.X = (float)e.X;
            m_AccelReading.Y = -(float)e.Y;
            m_AccelReading.Z = -(float)e.Z;
        }
        public void UnloadInput()
        {
            m_AccelSensor.Stop();
        }
    }
}
